package com.ptszyxx.popose.common.utils;

import android.app.ActivityManager;
import android.content.Context;
import android.util.Log;
import com.kwai.monitor.log.TurboAgent;
import com.kwai.monitor.log.TurboConfig;
import com.ptszyxx.popose.MyApp;
import com.ptszyxx.popose.common.enums.BannerEventEnum;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.commonsdk.listener.OnGetOaidListener;
import com.ysg.utils.YChannelUtil;
import com.ysg.utils.YSPUtils;
import com.ysg.utils.YStringUtil;

/**
 * 快手投放:日志搜索“KS_LOG_1.0.13”
 */
public class YMonitorUtil {
    private static YMonitorUtil instance;

    private YMonitorUtil(){}

    public static YMonitorUtil getInstance(){
        if (instance == null){
            synchronized (YMonitorUtil.class){
                if (instance == null) {
                    instance = new YMonitorUtil();
                }
            }
        }
        return instance;
    }


    /**
     * 初始化
     */
    public void init(Context context){
//        if (YChannelUtil.getInstance().kuaishou() && context.getPackageName().equals(getProcessName(context))) {
//            //初始化sdk,注意相关权限的申请，如IMEI权限，建议先进行申请权限后进行初始化
//            //未初始化SDK，调用的事件不会上报
//            TurboAgent.init(TurboConfig.TurboConfigBuilder.create(context)
//                    .setAppId("83708") //请填写平台申请的appId
//                    .setAppName("zhiyu") //请填写申请Appid时填写的appName
//                    .setEnableDebug(true)
//                    .build());
//        }

        getOAID();
    }

    public void getOAID(){
        UMConfigure.getOaid(MyApp.getContext(), oaid -> {
            if (YStringUtil.isNotEmpty(oaid)){
                YSPUtils.getInstance().saveOAID(oaid);
                Log.e("OAID", ""+oaid);
            }
        });
    }

    /**
     * 注册OAID并提交事件（注册，激活）
     */
    public void submit(BannerEventEnum eventEnum) {
        String oaid = YSPUtils.getInstance().getOAID();
        if (YStringUtil.isNotEmpty(oaid)){
            switch (eventEnum) {
                case register:
                    onRegister();
                    break;
                case active:
                    onActive();
                    break;
            }
            return;
        }

        UMConfigure.getOaid(MyApp.getContext(), new OnGetOaidListener() {
            @Override
            public void onGetOaid(String oaid) {
                if (YStringUtil.isNotEmpty(oaid)){
                    YSPUtils.getInstance().saveOAID(oaid);
                    Log.e("OAID", ""+oaid);
                    switch (eventEnum){
                        case register:
                            onRegister();
                            break;
                        case active:
                            onActive();
                            break;
                    }
                }
            }});
    }

    private String getProcessName(Context context) {
        String sCurProcessName = null;
        try {
            int pid = android.os.Process.myPid();
            ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            for (ActivityManager.RunningAppProcessInfo appProcess : mActivityManager.getRunningAppProcesses()) {
                if (appProcess.pid == pid) {
                    sCurProcessName = appProcess.processName;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sCurProcessName;
    }

    /**
     * 注册事件
     */
    private void onRegister(){
//        if (YChannelUtil.getInstance().kuaishou()){
//            TurboAgent.onRegister();
//        }
        if (YChannelUtil.getInstance().vivo()){
            onRegisterVivo(BannerEventEnum.register);
        }
        if (YChannelUtil.getInstance().oppo()){
            onOppo(BannerEventEnum.register);
        }
    }
    /**
     * 激活事件
     */
    private void onActive(){
//        if (YChannelUtil.getInstance().kuaishou()){
//            TurboAgent.onAppActive();
//        }
        if (YChannelUtil.getInstance().vivo()){
            onRegisterVivo(BannerEventEnum.active);
        }
        if (YChannelUtil.getInstance().oppo()){
            onOppo(BannerEventEnum.active);
        }
    }

    /**
     * 注册事件
     */
    private void onRegisterVivo(BannerEventEnum eventEnum){
            String oaid = YSPUtils.getInstance().getOAID();
//            HttpParams params = new HttpParams();
//            params.put("userId", oiad);
//            params.put("cvType", vivoEventEnum.getLabel());
//            EasyHttp.post(BaseNetWorkAllApi.vivoPush)
//                    .params(params)
//                    .execute(new SimpleCallBack<String>() {
//                        @Override
//                        public void onError(ApiException e) {
//                        }
//
//                        @Override
//                        public void onSuccess(String s) {
//                        }
//                    });
    }
    /**
     * 注册事件
     */
    private void onOppo(BannerEventEnum oppoEnum){
            String oaid = YSPUtils.getInstance().getOAID();
//            HttpParams params = new HttpParams();
//            params.put("oaid", oiad);
//            params.put("dataType", oppoEnum.getType());
//            EasyHttp.post(BaseNetWorkAllApi.oppoPush)
//                    .params(params)
//                    .execute(new SimpleCallBack<String>() {
//                        @Override
//                        public void onError(ApiException e) {
//                        }
//
//                        @Override
//                        public void onSuccess(String s) {
//                        }
//                    });
    }



}
